<template>
	<view class="body">
		<form @submit="formSubmit">
			<!-- 头像 -->
					<view class="face-wapper">
						<image src="../../static/icos/default-face.png" class="face"></image>
					</view>
				<!-- 昵称输入 -->
						<view class="info-wapper">
							<label class="words-lbl">昵称</label>
							<input name="nickname" type="text" value="" class="input" placeholder="请输入昵称" placeholder-class="graywords"/>
						</view>
				<!-- 账号输入 -->
					<view class="info-wapper" style="margin-top: 40upx;">
						<label class="words-lbl">账号</label>
						<input name="username" type="text" value="" class="input" placeholder="请输入电话号码" placeholder-class="graywords"/>
					</view>
					<!-- 密码输入 -->
					<view class="info-wapper" style="margin-top: 40upx;">
						<label class="words-lbl">密码</label>
						<input name="password" type="text" value="" password="true" class="input" placeholder="请输入密码" placeholder-class="graywords"/>
					</view>
					<!-- 身份信息 -->
					<view class="info-wapper" style="margin-top: 40upx;">
						<label class="words-lbl">ID</label>
						<input name="identity" type="text" value="" password="true" class="input" placeholder="身份证号码" placeholder-class="graywords"/>
					</view>
					<!-- 紧急联系人 -->
					<view class="info-wapper" v-if=" userType == '0' "    style="margin-top: 40upx;">
						<label class="words-lbl">电话</label>
						<input name="emergencyPhone" type="text" value="" password="true" class="input" placeholder="紧急联系人电话" placeholder-class="graywords"/>
					</view>
		<!--这是选择志愿者或者老人 -->
         <radio-group @change="radioChange">
						<view class="choooseid">
						<view><radio value="0"  />老人</view>
					      <view style="margin-left: 50upx;"><radio value="1" />志愿者 </view>
						  </view>
					  </label>
                  </radio-group>		
				<!-- 注册按钮 -->
		<button type="primary"  form-type="submit" style="margin-top: 60upx;width: 90%;">注册</button>
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//添加用户的表单
					nickname:'',
					username:'',
					password:'',
					userType:'',   //老人 或者志愿者
					identity:'',   //身份id
					emergencyPhone:''  //紧急联系人电话
			}
		},
		onLoad() {
			console.log("onlod");		
		},
		methods: {
			//这是选择身份
			radioChange(evt){
				console.log(evt);
			this.userType =	evt.detail.value;
			},
			//这是提交注册信息
			formSubmit(evt){
			console.log(evt)
			this.username =	evt.detail.value.username;
			this.nickname = evt.detail.value.nickname;
			this.password =	evt.detail.value.password;
			this.identity =	evt.detail.value.identity;
			this.emergencyPhone = evt.detail.value.emergencyPhone;
			//发起请求  第一代版本暂时没做数据格式校验 后期用uni-ui来写表单数据
			//发起注册请求
			uni.showLoading({
			    title: '正在为注册'  
			}); 
		 // 云开发注册
		 uniCloud.callFunction({
		     name:"user",
		 	data:{
					nickname : this.nickname,
					username : this.username,
					password : this.password,
					userType : this.userType,
					identity : this.identity,
					emergencyPhone : this.emergencyPhone			
		 	},
		 	success: (res) => {
					uni.hideLoading();
					uni.showToast({
						title: '注册成功',
						duration: 2000});	
					uni.redirectTo({
					    url:'../registerLogin/registerLogin'
					});
		 	},
		 	fail: (err) => {
		 		console.log(err);
				uni.hideLoading();
				uni.showToast({title: '注册失败'});
		 	}
		    
		 });
		 
		 
			}
		}
	}
</script>

<style>
	@import url("./register.css");
</style>
